package com.neepu.controller;



import com.neepu.pojo.MessageBoard;
import com.neepu.pojo.User;
import com.neepu.service.MessageBoardService;
import com.neepu.service.UserService;
import com.neepu.util.annotation.ArchivesLog;
import com.neepu.util.common.MyFileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private MessageBoardService messageBoardService;

    @ArchivesLog(operteContent = "账号管理")
    @RequestMapping("/manage")
    public ModelAndView manage(String id, ModelAndView mv){

        User user = userService.finUserById(id);
        mv.addObject("user",user);
        mv.setViewName("usermanage/manage");
        return mv;
    }

    @ArchivesLog(operteContent = "跳转修改密码")
    @RequestMapping("/password")
    public String updatapassword(){
        return "usermanage/password";
    }


    @ArchivesLog(operteContent = "跳转修改头像")
    @RequestMapping("/avatar")
    public String manageavatar(){
        return "usermanage/avatar";
    }

    @ArchivesLog(operteContent = "更新用户信息")
    @RequestMapping(value = "/updataUser")
    public ModelAndView updataUser(String userID, String userName, String email,
                                   String telephone, String gender, String major,
                                   String department, ModelAndView mv){

        User user = new User();
        user.setUserID(userID);
        user.setUserName(userName);
        user.setEmail(email);
        user.setTelephone(telephone);
        user.setGender(gender);
        user.setMajor(major);
        user.setDepartment(department);
        userService.updatePassword(user);
        mv.setViewName("redirect:manage");
        return mv;
    }

    @ArchivesLog(operteContent = "修改密码")
    @RequestMapping(value = "/toupdataPassword" )
    public ModelAndView toFindPassword(String userID, String pass, ModelAndView mv){
        User user = new User();
        user.setUserID(userID);
        user.setPassword(pass);
        userService.updatePassword(user);
        mv.setViewName("forward:loginForm");
        mv.addObject("message","你需要重新登陆系统！");
        return mv;
    }

    // 修改头像
    @ArchivesLog(operteContent = "修改头像")
    @RequestMapping(value="/uploadavatar", method = RequestMethod.POST)
    public String uploadavatar(@RequestParam("file") MultipartFile file,
                            HttpServletRequest request, String userID,  Model model) {
        String fileName = file.getOriginalFilename();
        String filePath = request.getSession().getServletContext().getRealPath("imgupload/");
        System.out.println(filePath+fileName);
        try {
            MyFileUtil.uploadFile(file.getBytes(), filePath, fileName);
        } catch (Exception e) {
            System.out.println("上传文件异常");
        }
        User user = new User();
        user.setUserID(userID);
        user.setAvatar("http://localhost:8080/Uces/imgupload/"+fileName);
        userService.updatePassword(user);
        model.addAttribute("message","您对头像更新成功！请重新登陆系统查看！");

        MessageBoard messageBoard = new MessageBoard();

        //返回json
        return "forward:/avatar";
    }
}
